package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.Scanner;

public class BibliotecaApp {

    ArrayList<Book> books = new ArrayList<Book>();
    User lib_user;

    public static void main(String[] args) {

        BibliotecaApp  app = new BibliotecaApp();
        System.out.println("\n============Welcome to Bangalore Public Library============\n");
        app.init();
        Scanner sc = new Scanner(System.in);
        int choice = 1;
        while(choice != 0)
        {
            app.user_menu();
            choice = sc.nextInt();
            app.process(choice);
        }
    }

    public void init()
    {
        lib_user = new User("2006116","Joan","880811");
        MyFileReader file = new MyFileReader("./src/com/twu/biblioteca/books.txt");
        ArrayList<String> file_content = new ArrayList<String>();
        file_content = file.ReadFile();
        for(String tmp:file_content) {
            Book book = new Book(tmp);
            books.add(book);
        }
    }

    public void user_menu()
    {
        System.out.println("********************");
        System.out.println("1.View books\n2.Reserve book\n3.Membership detail\n0.Quit");
        System.out.println("********************");
        System.out.println("Choice: ");
    }

    public void view_books(){
        System.out.println("\nAll the books in library as follow :\n");
        Book.display_head();
        for(Book tmp:books)
        {
            if(!tmp.hasOwner())
                tmp.display();
        }
    }

    public void invalidmsg()
    {
        System.out.println("Select a valid option!");
    }

    public Boolean reserve_book(String bookid)
    {

        for(Book tmp:books)
        {
            if(tmp.getId().equals(bookid)&&(!tmp.hasOwner()))
            {
                tmp.setOwner(lib_user.getId());
                System.out.println("Thank You! Enjoy the book.");
                return true;
            }
        }
      System.out.println("Sorry we don't have that book yet.");
      return false;
    }

    public void member_detail()
    {
        System.out.println("Please talk to a Librarian. Thank you.");
    }

    public boolean process(int choice)
    {
        switch (choice)
        {
            case 1 : view_books();break;
            case 2 : {
                      System.out.print("Input book id : ");
                      Scanner sc = new Scanner(System.in);
                      String bookid = sc.next();
                      reserve_book(bookid);break;
                     }
            case 3 : member_detail();break;
            case 0 : break;
            default: invalidmsg();return false;
        }
        return true;
    }
}
